package com.vpx.server.service.system.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vpx.server.bean.entity.Project;
import com.vpx.server.bean.entity.TestPrjTable;
import com.vpx.server.bean.info.system.SysDicValueInfo;
import com.vpx.server.bean.order.base.BaseDelOrder;
import com.vpx.server.bean.order.base.BaseIdStrOrder;
import com.vpx.server.bean.order.system.ProjectOrder;
import com.vpx.server.bean.order.system.ProjectPageOrder;
import com.vpx.server.bean.result.system.ProjectPageResult;
import com.vpx.server.bean.result.system.ProjectResult;
import com.vpx.server.bean.result.system.SysDicValuePageResult;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.framework.utils.DateUtils;
import com.vpx.server.mapper.system.ProjectMapper;
import com.vpx.server.mapper.system.TestPrjTableMapper;
import com.vpx.server.service.system.ProjectService;
import com.vpx.server.service.system.SqlExtService;
import com.vpx.server.service.system.SqlService;
import com.vpx.server.utils.UserLoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description
 * @Author PC
 * @Date 2023/11/18 16:31
 **/
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private SqlExtService sqlExtService;
    @Resource
    private SqlService sqlService;
    @Autowired
    private TestPrjTableMapper testPrjTableMapper;
    @Override
    public ResultMsg saveProject(ProjectOrder order) {
        Project project = new Project();
        BeanUtil.copyProperties(order,project);
        project.setCreateUser(UserLoginUtils.getCurrentUser() == null ? "admin" : UserLoginUtils.getCurrentUser().getId().toString());
        //project.setCreateUser("admin");
        project.setCreateTime(DateUtils.getDateTimeStr());
        save(project);
        TestPrjTable bproject =new TestPrjTable();
        bproject.setUuid(project.getId());
        String xmlPath=order.getProjectPath().replace("\\","/");
        bproject.setPrjfeilpath(xmlPath+"/"+order.getProjectName()+"/"+order.getProjectName()+".xml");
        bproject.setPrjname(order.getProjectName());
        bproject.setCreatepersion(UserLoginUtils.getCurrentUser() == null ? "admin" : UserLoginUtils.getCurrentUser().getId().toString());

        testPrjTableMapper.insert(bproject);
        //插入属性数据
        Map<String,Object> param = new HashMap<>();
        List<JSONObject> list = new ArrayList<>();
        JSONObject obj1 = JSONUtil.createObj();
        obj1.putByPath("id", IdUtil.fastSimpleUUID());
        obj1.putByPath("name", "硬件配置");
        obj1.putByPath("create_time", DateUtil.now());
        obj1.putByPath("project_id", project.getId());
        obj1.putByPath("config_type", "1");
        list.add(obj1);

        JSONObject obj2 = JSONUtil.createObj();
        obj2.putByPath("id", IdUtil.fastSimpleUUID());
        obj2.putByPath("name", "总线配置");
        obj2.putByPath("create_time", DateUtil.now());
        obj2.putByPath("project_id", project.getId());
        obj2.putByPath("config_type", "2");
        list.add(obj2);

        JSONObject obj3 = JSONUtil.createObj();
        obj3.putByPath("id", IdUtil.fastSimpleUUID());
        obj3.putByPath("name", "协议配置");
        obj3.putByPath("create_time", DateUtil.now());
        obj3.putByPath("project_id", project.getId());
        obj3.putByPath("config_type", "3");
        list.add(obj3);

        JSONObject obj4 = JSONUtil.createObj();
        obj4.putByPath("id", IdUtil.fastSimpleUUID());
        obj4.putByPath("name", "共享配置");
        obj4.putByPath("create_time", DateUtil.now());
        obj4.putByPath("project_id", project.getId());
        obj4.putByPath("config_type", "4");
        list.add(obj4);

        JSONObject obj5 = JSONUtil.createObj();
        obj5.putByPath("id", IdUtil.fastSimpleUUID());
        obj5.putByPath("name", "观察者配置");
        obj5.putByPath("create_time", DateUtil.now());
        obj5.putByPath("project_id", project.getId());
        obj5.putByPath("config_type", "5");
        list.add(obj5);



//        JSONObject obj6 = JSONUtil.createObj();
//        obj6.putByPath("id", IdUtil.fastSimpleUUID());
//        obj6.putByPath("name", "默认流程");
//        obj6.putByPath("create_time", DateUtil.now());
//        obj6.putByPath("project_id", project.getId());
//        obj6.putByPath("config_type", "6");
//        list.add(obj6);
        param.put("list",list);
        this.sqlService.execute("2",param);

        JSONObject projectInfo = JSONUtil.createObj();
        projectInfo.putByPath("id",project.getId());
        projectInfo.putByPath("name",project.getProjectName());
        projectInfo.putByPath("config_type","0");//0 项目 1、硬件配置 2、总线配置 3、协议配置 4、共享配置 5、观察者配置 6、流程
        projectInfo.putByPath("projectPath",order.getProjectPath());
        List<JSONObject> children = this.sqlExtService.findListObject("select id,name,config_type from c_project_attr where project_id = '"+project.getId()+"' order by id asc",JSONObject.class);
        projectInfo.putByPath("children",children);
        return ResultMsg.success(projectInfo);
    }

    @Override
    public ResultMsg deleteById(BaseDelOrder order) {
//        projectMapper.deleteById(order.getId());
        this.sqlExtService.execute("delete from c_project where id = '"+order.getId()+"'");
        this.sqlExtService.execute("delete from c_project_attr where project_id = '"+order.getId()+"'");
        this.sqlExtService.execute("delete from c_project_flow where project_id = '"+order.getId()+"'");
        this.sqlExtService.execute("delete from c_project_flow_instance where project_id = '"+order.getId()+"'");
        this.sqlExtService.execute("delete from c_base_info_attr where base_info_id in (SELECT bi.id from c_base_info bi where bi.project_id='"+order.getId()+"')");
        this.sqlExtService.execute("delete from c_base_info  where project_id = '"+order.getId()+"'");

        return ResultMsg.success();
    }

    @Override
    public ResultMsg updateProjectById(ProjectOrder order) {
        if (StrUtil.isEmpty(order.getId())){
            return ResultMsg.fail("数据id不能为空");
        }
        Project project = projectMapper.selectById(order.getId());
        if (project == null){
            return ResultMsg.fail("数据不存在，请刷新重试");
        }
        project.setProjectName(order.getProjectName());
        project.setProjectPath(order.getProjectPath());
        TestPrjTable bprjTable=new TestPrjTable();
        bprjTable.setPrjname(order.getProjectName());
        String xmlPath=order.getProjectPath().replace("\\","/");
        bprjTable.setPrjfeilpath(xmlPath+"/"+order.getProjectName()+"/"+order.getProjectName()+".xml");
        bprjTable.setUuid(order.getId());
        testPrjTableMapper.updateById(bprjTable);
        updateById(project);
        return ResultMsg.success();
    }

    @Override
    public ResultMsg<ProjectResult> selectProjectById(BaseIdStrOrder order) {
        if (StrUtil.isEmpty(order.getId())){
            return ResultMsg.fail("数据id不能为空");
        }
        Project project = projectMapper.selectById(order.getId());
        if (project == null){
            return ResultMsg.fail("数据不存在，请刷新重试");
        }
        ProjectResult result = new ProjectResult();
        BeanUtil.copyProperties(project,result);
        return ResultMsg.success(result);
    }

    @Override
    public ResultMsg<ProjectPageResult> pageQueryProject(ProjectPageOrder order) {
        PageHelper.startPage(order.getPageNum(), order.getPageSize());
        QueryWrapper<Project> wrapper = new QueryWrapper();
        wrapper.like(StrUtil.isNotBlank(order.getProjectName()),"project_name",order.getProjectName());
        wrapper.like(StrUtil.isNotBlank(order.getProjectPath()),"project_path",order.getProjectPath());
        List<Project> projects = projectMapper.selectList(wrapper);
        PageInfo<Project> pageInfo = new PageInfo<>(projects);
        PageInfo<ProjectResult> pageInfo2 = new PageInfo<>();
        BeanUtil.copyProperties(pageInfo,pageInfo2);
        ProjectPageResult result = new ProjectPageResult();
        result.setPageInfo(pageInfo2);
        return ResultMsg.success(result);

    }

    @Override
    public JSONObject getProjectInfo(String projectId) {
        JSONObject obj =  this.sqlExtService.findOneObject("select id,project_name,project_path  from c_project where id = '"+projectId+"' order by id asc",JSONObject.class);
        JSONObject projectInfo = JSONUtil.createObj();
        projectInfo.putByPath("id",projectId);
        projectInfo.putByPath("name",obj.getStr("project_name"));
        projectInfo.putByPath("config_type","0");//0 项目 1、硬件配置 2、总线配置 3、协议配置 4、共享配置 5、观察者配置 6、流程
        projectInfo.putByPath("projectPath",obj.getStr("project_path"));
        List<JSONObject> children = this.sqlExtService.findListObject("select id,name,config_type from c_project_attr where project_id = '"+projectId+"' order by id asc",JSONObject.class);
        projectInfo.putByPath("children",children);

        return projectInfo;
    }

}
